// Copyright  2012 Petr TALLA. [petr.talla@gmail.com]
//
// This file is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License
// as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
//
// This file is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY;  without even the implied warranty
// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// See the GNU Lesser General Public License for more details.
// [http://www.gnu.org/copyleft/lesser.html]
//=============================================================================
#include "./TcCmds_xobject.h"
#include "./TcXobject.h"
#include "./TcXattrSTR.h"
#include "./TcXdb.h"
#include <TcArgCol.h>
#include <TcArgVal.h>
#include <TcObjectRegistry.h>
#include <cstdio>
#include <cstring>
#include <cstdlib>

//=============================================================================
bool TcCmds_xobject::registrator_ = TcCmds_xobject::registerCmds_();

//=============================================================================
TcCmds_xobject::TcCmds_xobject()
{
}

//=============================================================================
int TcCmds_xobject::xdb_create(TcCmdEngine* /*engine*/, TcArgCol& args)
//xdb_create -name name -saver
{   const char* name = args.value("name", "__sys__null");

    TcXdb* xdb = new TcXdb(name);

    if ( strcmp( name, "__sys__null") != 0 )
    {   xdb->nameSet(name);
    }

    if ( args.getVal("saver") )
    {   TcArgCol col;
        col.appendVal("storage_source_set", "");
        col.appendVal(xdb->id(), "");
        TcCmdEngine::engine().execute(&col, false);
    }

    args.appendVal(xdb->id(), "result");
    return 1;
}

//=============================================================================
int TcCmds_xobject::xobject_create(TcCmdEngine* /*engine*/, TcArgCol& args)
//xobject_create $category|null -name1 $value1 -name2 $value2
{  TcXobject* xobj = new TcXobject();

   for (long i = 2; i < args.count(); i++)
   {   TcArgVal* vali = args.at(i)->getAsVal();
       if ( vali->name()[0] == '_') continue;
       TcXattrSTR* attrSTR = new TcXattrSTR(vali->name(), vali->value());
       xobj->append(attrSTR);
   }

   if ( TcXdb::active() != NULL )
   {    TcXdb::active()->append(xobj);
   }

   args.appendVal(xobj->id(), "result");
   return 1;
}

//=============================================================================
int TcCmds_xobject::xattr_create(TcCmdEngine* /*engine*/, TcArgCol& args)
//xattr_create str id -name $name -value $value
{  if ( args.count() < 3 ) return args.appendError("xattr_create command expects at least attribute type and object id");

   TcArgVal* type = args.at(1)->getAsVal();
   if (type == NULL) return args.appendError("attribute type is expected as second argument");
   if ( strcmp(type->value(), "str") != 0 ) return args.appendError("only string argument is supported now");

   TcArgVal* id = args.at(2)->getAsVal();
   if (id == NULL) return args.appendError("object id is expected as second argument");
   int ID = atoi(id->value());
   TcXobject* object = TcObjectRegistry::registry().findt<TcXobject>(ID);
   if(object == NULL) return args.appendError("object id=%d not found", ID);

   TcXattrSTR* attr = new TcXattrSTR(args.value("name", ""), args.value("value", ""));
   object->append(attr);

   return 0;
}

 //=============================================================================
